//2009/08/07 21:49:10
#include <string>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;

class SMBus
{
public:
    int transmitTime(vector <string> msg, vector <int> times)
    {
        vector<pair<string, int> > v(msg.size());
        for (int i=0; i<msg.size(); i++)
        {
            v[i] = make_pair(msg[i], times[i]);
        }
		sort(v.begin(), v.end());
		int timecost = 0;
		for(int i=0; i<v.size(); i++)
		{
			int end = v.size();
			for(int j=0; j<v[i].first.size(); j++)
			{
				int byteTime = v[i].second;
				for(int k=i+1; k<end; k++)
				{
					byteTime = max(byteTime, v[k].second);
				}
				timecost += byteTime;
				for(int k=i+1; k<end; k++)
				{
					if(v[i].first[j] != v[k].first[j])
						end = k;
				}
			}
		}
		return timecost;
    }
};
